/*
 * @(#)BulkUtils.java   1.0   May 18, 2007
 *
 * Copyright 2000-2007 ETH Zurich. All Rights Reserved.
 *
 * This software is the proprietary information of ETH Zurich.
 * Use is subject to license terms.
 *
 * @(#) $Id: CollectionUtils.java 425 2007-05-19 23:12:27Z D\aldespin $
 */
package ch.ethz.globis.javaom.utils;

import ch.ethz.globis.javaom.core.AttributeValue;
import ch.ethz.globis.javaom.core.Bulk;
import ch.ethz.globis.javaom.core.Object;
import ch.ethz.globis.javaom.system.Metadata;

/**
 * @author aldespin
 * @version 1.0
 */
public class CollectionUtils {

   public static boolean add(final Object collection, final Object member) {
      return CollectionUtils.getExtent(collection).add(member);
   }

   public static boolean remove(final Object collection, final Object member) {
      return CollectionUtils.getExtent(collection).remove(member);
   }

   @SuppressWarnings("unchecked")
   public static AttributeValue<String> getName(final Object collection) {

      final Bulk argumentHasAttributeValues = ObjectUtils.getAttributeValues(collection);

      // get name attribute object
      final Object nameAttribute = Metadata.METADATA.getAttributeObject(Metadata.COLLECTION,
            Metadata.NAME);

      // get links of name attribute
      final Bulk nameValues = AttributeUtils.getAttributeValues(argumentHasAttributeValues,
            nameAttribute);

      // test if not empty
      if (nameValues.isEmpty()) {
         throw new RuntimeException("Collection '" + collection.toString()
               + "' given has no name attribute value");
      }
      // test if no more than one value
      if (nameValues.size() > 1) {
         throw new RuntimeException("Collection '" + collection.toString()
               + "' given has more than one name attribute value");
      }

      // return result
      try {
         return (AttributeValue<String>) nameValues.iterator().next();
      } catch (final ClassCastException e) {
         throw new RuntimeException("Collection '" + collection.toString()
               + "' given has a name attribute value of type '"
               + nameValues.iterator().next().getClass().getSimpleName()
               + "' instead of AttributeValue<String>");
      }
   }

   public static Object getMemberType(final Object collection) {

      final Bulk argumentHasAttributeValues = ObjectUtils.getAttributeValues(collection);

      // get membertype attribute object
      final Object memberTypeAttribute = Metadata.METADATA.getAttributeObject(Metadata.COLLECTION,
            Metadata.MEMBERTYPE);

      // get links of membertype attribute
      final Bulk membertypeValues = AttributeUtils.getAttributeValues(argumentHasAttributeValues,
            memberTypeAttribute);

      // test if not empty
      if (membertypeValues.isEmpty()) {
         throw new RuntimeException("Collection '" + collection.toString()
               + "' given has no membertype attribute value");
      }
      // test if no more than one value
      if (membertypeValues.size() > 1) {
         throw new RuntimeException("Collection '" + collection.toString()
               + "' given has more than one membertype attribute value");
      }

      // return result
      return membertypeValues.iterator().next();
   }

   public static Bulk getExtent(final Object collection) {
      
      final Bulk argumentHasAttributeValues = ObjectUtils.getAttributeValues(collection);

      // get extent attribute object
      final Object extentAttribute = Metadata.METADATA.getAttributeObject(Metadata.COLLECTION,
            Metadata.EXTENT);

      // get links of extent attribute
      final Bulk extentValues = AttributeUtils.getAttributeValues(argumentHasAttributeValues,
            extentAttribute);

      // test if not empty
      if (extentValues.isEmpty()) {
         throw new RuntimeException("Collection '" + collection.toString()
               + "' given has no extent attribute value");
      }
      // test if no more than one value
      if (extentValues.size() > 1) {
         throw new RuntimeException("Binary Collection '" + collection.toString()
               + "' given has more than one extent attribute value");
      }

      // return result
      try {
         return (Bulk) extentValues.iterator().next();
      } catch (final ClassCastException e) {
         throw new RuntimeException("Collection '" + collection.toString()
               + "' given has an extent attribute value of type '"
               + extentValues.iterator().next().getClass().getSimpleName() + "' instead of Bulk");
      }
   }
}
